<template>
    <div class="selectWrap">
        <a-form-item :label="item.label">
            <a-tooltip :title="tooltipFormat">
                <a-select :allowClear="true" :loading="loading"
                    v-decorator="[item.dataIndex, { rules: [{ required: item.required, message: getMessage(item) }] }]"
                    :options="selectList" :mode="item.mode" :disabled="item.disabled || disabledFarmet || loading"
                    :placeholder="getMessage(item)"></a-select>
            </a-tooltip>
        </a-form-item>
    </div>
</template>
<script>
import { getMessage } from './js/js';

export default {
    name: 'FormSelectUrl',
    props: {
        item: {
            type: Object,
            default: () => { }
        },
        formVal: {
            type: Object,
            default: () => { }
        },
        formData: {
            type: [Array, Object],
            default: () => []
        },
        form: {
            type: [Array, Object, String],
        }
    },
    data() {
        return {
            selectList: [],
            loading: false,
            oldV: undefined,
        }
    },
    methods: {
        getMessage,
        getList() {
            if (this.item && this.item.associationDataIndex) {
                if (!this.formVal['id']) {
                    this.$nextTick(() => {
                        this.form.setFieldsValue({
                            ...this.formVal,
                            [this.item.dataIndex]: undefined,
                        })
                    })
                }
                let v = this.formVal[this.item.associationDataIndex];
                if (v) {
                    this.loading = true;
                    let params = {
                        [this.item.associationDataIndex]: v
                    };
                    let url = this[`$${this.item.urlName}`]
                    this.getAxios(url, params).then((res) => {
                        if (res.code == 200) {
                            this.selectList = res.data.map(v => {
                                let obj = {};
                                if (this.item.dataIndex == 'flowTypeCode') {
                                    obj['label'] = v.flowTypeName;
                                    obj['value'] = v.flowTypeCode;
                                } else if (this.item.dataIndex == 'leadTime') {
                                    obj['label'] = `${v}分钟`;
                                    obj['value'] = v;
                                }
                                return obj;
                            })
                            if (this.item.dataIndex == 'leadTime') {
                                let items = this.selectList.find(v => v.value == this.formVal[this.item.dataIndex])
                                if (!items) {
                                    this.form.setFieldsValue({
                                        ...this.formVal,
                                        [this.item.dataIndex]: undefined,
                                    })
                                }
                            }
                        }
                    }).catch(() => {
                    }).finally(() => {
                        this.loading = false;
                    })
                }
            }

        },
    },
    computed: {
        disabledFarmet() {
            let res = true;
            if (this.item && this.item.associationDataIndex) {
                let v = this.formVal[this.item.associationDataIndex];
                if (v) {
                    res = false;
                    if (this.oldV != v) {
                        this.getList();
                    }
                    this.oldV = v;
                } else {
                    res = true;
                    this.oldV = undefined;
                }
            }
            return res;
        },
        tooltipFormat() {
            let res = '';
            if (this.disabledFarmet) {
                let i = this.formData.find(v => v.dataIndex == this.item.associationDataIndex);
                if (['select', "selectx"].includes(i.type)) {
                    res = `请先选择`
                } else {
                    res = `请先输入`
                }
                res += i.label;
            }
            return res;
        },
        // selectList() {
        //     let res = [];
        //     if (this.item && this.item.associationDataIndex) {
        //         let v = this.formVal[this.item.associationDataIndex];
        //         if (v) {
        //             res = [];
        //         }
        //     }
        //     return res;
        // }
    },
    watch: {
        formVal: {
            handler(nVal, oVal) {
            },
            immediate: true,
            deep: true,
        }
    }
}
</script>
<style lang="less" scoped>
.selectWrap {}
</style>